/**
 *
 */
package kevin.study.zephyr.internal;

import java.util.Observable;

import kevin.study.zephyr.domain.dao.IDAOFactory;
import kevin.study.zephyr.domain.dao.IGenericDAO;
import kevin.study.zephyr.platform.Platform;

/**
 * 管理DB,DAO的类 这个类是被观察者
 *
 * @author Kevin.Huang
 * @date 2007-9-27
 */
public final class DBManager extends Observable {

	private IDAOFactory daoFactory;

	private DBManager() {
		super();
	}

	static final class DBManagerHolder {

		private DBManagerHolder() {
		}

		static final DBManager SINGLETON = new DBManager();
		// 初始化DBManager,添加观察者.
		static {
			SINGLETON.addObserver(Platform.getDocumentManager());
			SINGLETON.addObserver(Platform.getFormManager());
			SINGLETON.addObserver(Platform.getViewManager());
		}
	}

	/**
	 * 获取DBManager实例
	 *
	 * @return
	 */
	public static DBManager getDefault() {
		return DBManagerHolder.SINGLETON;
	}

	/**
	 * 获得DAO
	 *
	 * @return the daoClass
	 */
	public <K> IGenericDAO<K> getDAO(Class<K> clazz) {
		if (daoFactory == null) {
			throw new RuntimeException("DAO Factory is missing!");
		}
		return daoFactory.createDAO(clazz);
	}

	/**
	 * @param daoFactory
	 *            the daoFactory to set
	 */
	public void setDaoFactory(IDAOFactory daoFactory) {
		this.daoFactory = daoFactory;
		notifyObservers();
	}

}
